package com.hl.card.modular.packBack.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.plugins.Page;
import com.hl.card.common.constant.factory.PageFactory;
import com.hl.card.common.persistence.model.DisMemberApply;
import com.hl.card.common.persistence.model.HotelOrder;
import com.hl.card.common.util.DateUtils;
import com.hl.card.modular.packBack.service.HotelOrderService;


@Controller
@RequestMapping("/hotelOrder")
public class HotelOrderController {
	private String PREFIX = "/packback/order/";
	@Autowired
	private HotelOrderService<HotelOrder> hotelOrderService;
	
	
    @RequestMapping("")
    public String index(){
        return PREFIX+"hotel_order.html";
    }
    
    @RequestMapping("/list")
    @ResponseBody
	public Object configList(HotelOrder  order)
	{
		Page<HotelOrder> page = new PageFactory<HotelOrder>().defaultPage();
		List<HotelOrder> list=hotelOrderService.list(page,order);
		Map<String,Object> result=new HashMap<String,Object>();
		result.put("rows", list);
		result.put("total", page.getTotal());
		return result;
	}
    
    
    /**
	 * 导出
	 */
	@RequestMapping(value = "/exPort")
	@ResponseBody
	public void exPort(HotelOrder  order,HttpServletResponse response) {
		Map<String,Object> map=new HashMap<String, Object>();
	    map.put("start_time", order.getStart_time()+" 00:00:00");
	    map.put("end_time", order.getEnd_time()+" 59:59:59");
	    List<HotelOrder> list=hotelOrderService.exPort(map);
		// 声明一个头部信息
		List<String> headers = Arrays.asList("订单号", "创建时间", "异地订单号", "酒店名称",
				"入住时间", "离店时间", "房间数", "支付总价", "订单状态","联系人", "联系手机");
		// 声明一个工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 生成一个表格
		HSSFSheet sheet = workbook.createSheet();
		sheet.setDefaultColumnWidth((short) 24);
		HSSFCellStyle style = workbook.createCellStyle();
		style.setBorderBottom(BorderStyle.THIN); // 下边框
		style.setBorderLeft(BorderStyle.THIN);// 左边框
		style.setBorderTop(BorderStyle.THIN);// 上边框
		style.setBorderRight(BorderStyle.THIN);// 右边框
		style.setAlignment(HorizontalAlignment.CENTER); // 居中
		style.setFillForegroundColor((short) 13);// 设置背景色
		style.setFillPattern(FillPatternType.SOLID_FOREGROUND);

		HSSFCellStyle style2 = workbook.createCellStyle();
		style2.setBorderBottom(BorderStyle.THIN); // 下边框
		style2.setBorderLeft(BorderStyle.THIN);// 左边框
		style2.setBorderTop(BorderStyle.THIN);// 上边框
		style2.setBorderRight(BorderStyle.THIN);// 右边框
		style2.setAlignment(HorizontalAlignment.CENTER); // 居中
		HSSFRow row = sheet.createRow(0);

		for (String head : headers) {
			HSSFCell cell = row.createCell(headers.indexOf(head));
			HSSFRichTextString richString = new HSSFRichTextString(head);
			HSSFFont font3 = workbook.createFont();
			font3.setBold(true);
			richString.applyFont(font3);
			cell.setCellValue(richString);
			cell.setCellStyle(style);

		}
		for (int i = 0; i < list.size(); i++) {
			row = sheet.createRow(i + 1);
			HotelOrder o= list.get(i);
			int j=0;
			HSSFCell cell = row.createCell(j);
			String textValue = o.getOrder_number().toString();
			HSSFRichTextString richString = new HSSFRichTextString(textValue);  //订单编号
			cell.setCellValue(richString);
			cell.setCellStyle(style2);
			j++;
			
			HSSFCell cell2 = row.createCell(j);
			String textValue2 =DateUtils.DateToStr(o.getAdd_time(),"yyyy-MM-dd HH:mm:ss");
			HSSFRichTextString richString2 = new HSSFRichTextString(textValue2);   //创建时间
			cell2.setCellValue(richString2);
			cell2.setCellStyle(style2);
			j++;
			
			HSSFCell cell3 = row.createCell(j);
			String textValue3 = o.getApi_order_number();   //异地订单号
			HSSFRichTextString richString3 = new HSSFRichTextString(textValue3);
			cell3.setCellValue(richString3);
			cell3.setCellStyle(style2);
			j++;
			
			
			HSSFCell cell4 = row.createCell(j);
			String textValue4 = o.getHotel_name();   //酒店名称
			HSSFRichTextString richString4 = new HSSFRichTextString(textValue4);
			cell4.setCellValue(richString4);
			cell4.setCellStyle(style2);
			j++;
			
			HSSFCell cell5 = row.createCell(j);
			
			String textValue5 = DateUtils.DateToStr(o.getCheck_in_time(),"yyyy-MM-dd HH:mm:ss");  //入住时间
			HSSFRichTextString richString5 = new HSSFRichTextString(textValue5);
			cell5.setCellValue(richString5);
			cell5.setCellStyle(style2);
			j++;
			
			HSSFCell cell6 = row.createCell(j);
			String textValue6 = DateUtils.DateToStr(o.getCheck_out_time(),"yyyy-MM-dd HH:mm:ss");   //离店时间
			HSSFRichTextString richString6 = new HSSFRichTextString(textValue6);
			cell6.setCellValue(richString6);
			cell6.setCellStyle(style2);
			j++;
			
			HSSFCell cell7 = row.createCell(j);
			String textValue7 = o.getBook_number()+"";   //房间数
			HSSFRichTextString richString7 = new HSSFRichTextString(textValue7);
			cell7.setCellValue(richString7);
			cell7.setCellStyle(style2);
			j++;
			
			HSSFCell cell8 = row.createCell(j);
			String textValue8 = o.getPay_price().toString();   //支付总价
			HSSFRichTextString richString8 = new HSSFRichTextString(textValue8);
			cell8.setCellValue(richString8);
			cell8.setCellStyle(style2);
			j++;
			
			HSSFCell cell9 = row.createCell(j);
			Integer status=o.getStatus();
			String status_txt="";
	         if(status.equals(50))
	         {
	        	 status_txt="待支付";
	         }else if(status.equals(60))
	         {
	        	 status_txt="待确认";
	         }else if(status.equals(70))
	         {
	        	 status_txt="已确认";
	         }else if(status.equals(80))
	         {
	        	 status_txt="退订中";
	         }else if(status.equals(508))
	         {
	        	 status_txt="已退订";
	         }else if(status.equals(90))
	         {
	        	 status_txt="已取消";
	         }
			
			String textValue9 = status_txt;   //订单状态
			HSSFRichTextString richString9 = new HSSFRichTextString(textValue9);
			cell9.setCellValue(richString9);
			cell9.setCellStyle(style2);
			j++;
			
			
			HSSFCell cell12 = row.createCell(j);
			String textValue12 = o.getName();   //联系人
			HSSFRichTextString richString12 = new HSSFRichTextString(textValue12);
			cell12.setCellValue(richString12);
			cell12.setCellStyle(style2);
			j++;
			
			HSSFCell cell10 = row.createCell(j);
			String textValue10 = o.getPhone();   //联系手机
			HSSFRichTextString richString10 = new HSSFRichTextString(textValue10);
			cell10.setCellValue(richString10);
			cell10.setCellStyle(style2);
			j++;
		}

		response.setContentType("application/octet-stream");
		try{
		response.setHeader("Content-disposition", "attachment;filename="
				+ new String(("酒店订单记录.xls").getBytes("UTF-8"), "ISO8859-1"));// 默认Excel名称
		response.flushBuffer();
		workbook.write(response.getOutputStream());
		}catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
    
}
